package no.uib.olsdialog.example;

import com.jgoodies.looks.plastic.PlasticLookAndFeel;
import com.jgoodies.looks.plastic.PlasticXPLookAndFeel;
import com.jgoodies.looks.plastic.theme.SkyKrupp;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import no.uib.olsdialog.OLSDialog;
import no.uib.olsdialog.OLSInputable;

/**
 * An example of how the OLS Dialog can be used.
 *
 * @author Harald Barsnes
 */
public class OLS_Example extends javax.swing.JFrame implements OLSInputable {

    /**
     * Creates a new OLS_Example frame and makes it visible.
     */
    public OLS_Example() {
        // set the font color for the titlted borders, looks better than the default black
        UIManager.put("TitledBorder.titleColor", new Color(59, 59, 59));
        initComponents();
        processingMethodsJScrollPane.getViewport().setOpaque(false);
        processingMethodsJTable.getColumn(" ").setMaxWidth(40);
        processingMethodsJTable.getColumn(" ").setMinWidth(40);
        setLocationRelativeTo(null);
    }

    /**
     * Sets the look and feel of the OLS Dialog.
     *
     * Note that the OLS Dialog has been created with the following look and
     * feel in mind. If using a different look and feel you might need to tweak
     * the GUI to get the best appearance.
     */
    private static void setLookAndFeel() {

        boolean nimbusLookAndFeelFound = false;

        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    nimbusLookAndFeelFound = true;
                    break;
                }
            }
        } catch (Exception e) {
            // ignore error, use look and feel below
        }

        if (!nimbusLookAndFeelFound) {
            try {
                PlasticLookAndFeel.setPlasticTheme(new SkyKrupp());
                UIManager.setLookAndFeel(new PlasticXPLookAndFeel());
            } catch (Exception ex) {
                // this should not be possible...
                // use default look and feel
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        processingMethodsPopupJMenu = new javax.swing.JPopupMenu();
        processingMethodsEditJMenuItem = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JSeparator();
        processingMethodsDeleteSelectedRowJMenuItem = new javax.swing.JMenuItem();
        backgroundPanel = new javax.swing.JPanel();
        textFieldExamplePanel = new javax.swing.JPanel();
        sourceLabel = new javax.swing.JLabel();
        detectorLabel = new javax.swing.JLabel();
        instrumentDetectorJTextField = new javax.swing.JTextField();
        instrumentSourceJTextField = new javax.swing.JTextField();
        instrumentSourceJButton = new javax.swing.JButton();
        instrumentDetectorJButton = new javax.swing.JButton();
        tableExamplePanel = new javax.swing.JPanel();
        processingMethodsJScrollPane = new javax.swing.JScrollPane();
        processingMethodsJTable = new javax.swing.JTable();
        processingMethodsJButton = new javax.swing.JButton();
        closeJButton = new javax.swing.JButton();
        psiModExamplePanel = new javax.swing.JPanel();
        massLabel = new javax.swing.JLabel();
        modificationMassJTextField = new javax.swing.JTextField();
        modificationMassJButton = new javax.swing.JButton();
        plusMinusLabel = new javax.swing.JLabel();
        modificationAccuracyJTextField = new javax.swing.JTextField();
        psiModLabel = new javax.swing.JLabel();
        modificationJTextField = new javax.swing.JTextField();
        nameLabel = new javax.swing.JLabel();
        modificationNameJTextField = new javax.swing.JTextField();

        processingMethodsEditJMenuItem.setMnemonic('E');
        processingMethodsEditJMenuItem.setText("Edit");
        processingMethodsEditJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                processingMethodsEditJMenuItemActionPerformed(evt);
            }
        });
        processingMethodsPopupJMenu.add(processingMethodsEditJMenuItem);
        processingMethodsPopupJMenu.add(jSeparator4);

        processingMethodsDeleteSelectedRowJMenuItem.setText("Delete");
        processingMethodsDeleteSelectedRowJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                processingMethodsDeleteSelectedRowJMenuItemActionPerformed(evt);
            }
        });
        processingMethodsPopupJMenu.add(processingMethodsDeleteSelectedRowJMenuItem);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("OLS Dialog - Usage Example");
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().
            getResource("/no/uib/olsdialog/icons/ols_transparent_small.GIF")));

    backgroundPanel.setBackground(new java.awt.Color(230, 230, 230));

    textFieldExamplePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Example Using Text Fields"));
    textFieldExamplePanel.setOpaque(false);

    sourceLabel.setText("Source");

    detectorLabel.setText("Detector");

    instrumentDetectorJTextField.setEditable(false);
    instrumentDetectorJTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

    instrumentSourceJTextField.setEditable(false);
    instrumentSourceJTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

    instrumentSourceJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/olsdialog/icons/ols_transparent.GIF"))); // NOI18N
    instrumentSourceJButton.setToolTipText("Ontology Lookup Service");
    instrumentSourceJButton.setBorderPainted(false);
    instrumentSourceJButton.setPreferredSize(new java.awt.Dimension(61, 23));
    instrumentSourceJButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            instrumentSourceJButtonActionPerformed(evt);
        }
    });

    instrumentDetectorJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/olsdialog/icons/ols_transparent.GIF"))); // NOI18N
    instrumentDetectorJButton.setToolTipText("Ontology Lookup Service");
    instrumentDetectorJButton.setBorderPainted(false);
    instrumentDetectorJButton.setPreferredSize(new java.awt.Dimension(61, 23));
    instrumentDetectorJButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            instrumentDetectorJButtonActionPerformed(evt);
        }
    });

    org.jdesktop.layout.GroupLayout textFieldExamplePanelLayout = new org.jdesktop.layout.GroupLayout(textFieldExamplePanel);
    textFieldExamplePanel.setLayout(textFieldExamplePanelLayout);
    textFieldExamplePanelLayout.setHorizontalGroup(
        textFieldExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(textFieldExamplePanelLayout.createSequentialGroup()
            .addContainerGap()
            .add(textFieldExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(sourceLabel)
                .add(detectorLabel))
            .add(18, 18, 18)
            .add(textFieldExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, textFieldExamplePanelLayout.createSequentialGroup()
                    .add(instrumentDetectorJTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                    .add(instrumentDetectorJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 36, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(org.jdesktop.layout.GroupLayout.TRAILING, textFieldExamplePanelLayout.createSequentialGroup()
                    .add(instrumentSourceJTextField)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                    .add(instrumentSourceJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 36, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap())
    );
    textFieldExamplePanelLayout.setVerticalGroup(
        textFieldExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(textFieldExamplePanelLayout.createSequentialGroup()
            .add(2, 2, 2)
            .add(textFieldExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                .add(sourceLabel)
                .add(instrumentSourceJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(instrumentSourceJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(textFieldExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                .add(detectorLabel)
                .add(instrumentDetectorJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(instrumentDetectorJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    tableExamplePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Example Using Tables"));
    tableExamplePanel.setOpaque(false);

    processingMethodsJTable.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            " ", "CV Terms", "Value"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.Integer.class, java.lang.String.class, java.lang.String.class
        };
        boolean[] canEdit = new boolean [] {
            false, false, true
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    processingMethodsJTable.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            processingMethodsJTableMouseClicked(evt);
        }
    });
    processingMethodsJTable.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            processingMethodsJTableKeyReleased(evt);
        }
    });
    processingMethodsJScrollPane.setViewportView(processingMethodsJTable);

    processingMethodsJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/olsdialog/icons/ols_transparent.GIF"))); // NOI18N
    processingMethodsJButton.setText("Add Processing Method");
    processingMethodsJButton.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
    processingMethodsJButton.setPreferredSize(new java.awt.Dimension(177, 23));
    processingMethodsJButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            processingMethodsJButtonActionPerformed(evt);
        }
    });

    org.jdesktop.layout.GroupLayout tableExamplePanelLayout = new org.jdesktop.layout.GroupLayout(tableExamplePanel);
    tableExamplePanel.setLayout(tableExamplePanelLayout);
    tableExamplePanelLayout.setHorizontalGroup(
        tableExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(tableExamplePanelLayout.createSequentialGroup()
            .addContainerGap()
            .add(tableExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                .add(org.jdesktop.layout.GroupLayout.LEADING, processingMethodsJButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(processingMethodsJScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE))
            .addContainerGap())
    );
    tableExamplePanelLayout.setVerticalGroup(
        tableExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(tableExamplePanelLayout.createSequentialGroup()
            .addContainerGap()
            .add(processingMethodsJScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 85, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(processingMethodsJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    closeJButton.setText("Close");
    closeJButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            closeJButtonActionPerformed(evt);
        }
    });

    psiModExamplePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Example Using PSI-MOD"));
    psiModExamplePanel.setOpaque(false);

    massLabel.setText("Mass");

    modificationMassJTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    modificationMassJTextField.setText("15.994915");

    modificationMassJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/olsdialog/icons/ols_transparent.GIF"))); // NOI18N
    modificationMassJButton.setToolTipText("Ontology Lookup Service");
    modificationMassJButton.setBorderPainted(false);
    modificationMassJButton.setPreferredSize(new java.awt.Dimension(61, 23));
    modificationMassJButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            modificationMassJButtonActionPerformed(evt);
        }
    });

    plusMinusLabel.setText("+-");

    modificationAccuracyJTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    modificationAccuracyJTextField.setText("0.1");

    psiModLabel.setText("PSI-MOD");

    modificationJTextField.setEditable(false);
    modificationJTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

    nameLabel.setText("Name");

    modificationNameJTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    modificationNameJTextField.setText("Oxidation");

    org.jdesktop.layout.GroupLayout psiModExamplePanelLayout = new org.jdesktop.layout.GroupLayout(psiModExamplePanel);
    psiModExamplePanel.setLayout(psiModExamplePanelLayout);
    psiModExamplePanelLayout.setHorizontalGroup(
        psiModExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(psiModExamplePanelLayout.createSequentialGroup()
            .addContainerGap()
            .add(psiModExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(psiModExamplePanelLayout.createSequentialGroup()
                    .add(psiModExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(massLabel)
                        .add(nameLabel))
                    .add(30, 30, 30))
                .add(org.jdesktop.layout.GroupLayout.TRAILING, psiModExamplePanelLayout.createSequentialGroup()
                    .add(psiModLabel)
                    .add(18, 18, 18)))
            .add(psiModExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(psiModExamplePanelLayout.createSequentialGroup()
                    .add(psiModExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(psiModExamplePanelLayout.createSequentialGroup()
                            .add(modificationMassJTextField)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                            .add(plusMinusLabel)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                            .add(modificationAccuracyJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 68, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(modificationNameJTextField))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                    .add(modificationMassJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 36, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(modificationJTextField))
            .addContainerGap())
    );
    psiModExamplePanelLayout.setVerticalGroup(
        psiModExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(psiModExamplePanelLayout.createSequentialGroup()
            .add(psiModExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(psiModExamplePanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .add(psiModExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(modificationMassJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(modificationAccuracyJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(plusMinusLabel)
                        .add(massLabel))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(psiModExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(modificationNameJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(nameLabel)))
                .add(psiModExamplePanelLayout.createSequentialGroup()
                    .add(21, 21, 21)
                    .add(modificationMassJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(psiModExamplePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(psiModLabel)
                .add(modificationJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    org.jdesktop.layout.GroupLayout backgroundPanelLayout = new org.jdesktop.layout.GroupLayout(backgroundPanel);
    backgroundPanel.setLayout(backgroundPanelLayout);
    backgroundPanelLayout.setHorizontalGroup(
        backgroundPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(backgroundPanelLayout.createSequentialGroup()
            .addContainerGap()
            .add(backgroundPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                .add(psiModExamplePanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(tableExamplePanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(textFieldExamplePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, closeJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 87, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addContainerGap())
    );
    backgroundPanelLayout.setVerticalGroup(
        backgroundPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(backgroundPanelLayout.createSequentialGroup()
            .addContainerGap()
            .add(textFieldExamplePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(tableExamplePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(psiModExamplePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(closeJButton)
            .addContainerGap())
    );

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(backgroundPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(backgroundPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    );

    pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Opens the OLS dialog.
     *
     * @param evt
     */
    private void instrumentSourceJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_instrumentSourceJButtonActionPerformed
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        String searchTerm = null;
        String ontology = "PSI Mass Spectrometry Ontology [MS] / source";

        if (instrumentSourceJTextField.getText().length() > 0) {

            searchTerm = instrumentSourceJTextField.getText();

            ontology = searchTerm.substring(searchTerm.lastIndexOf("[") + 1, searchTerm.lastIndexOf("]") - 1);
            ontology = getOntologyFromCvTerm(ontology);

            searchTerm = instrumentSourceJTextField.getText().substring(0, instrumentSourceJTextField.getText().indexOf("[") - 1);
            searchTerm = searchTerm.replaceAll("-", " ");
            searchTerm = searchTerm.replaceAll(":", " ");
            searchTerm = searchTerm.replaceAll("\\(", " ");
            searchTerm = searchTerm.replaceAll("\\)", " ");
            searchTerm = searchTerm.replaceAll("&", " ");
            searchTerm = searchTerm.replaceAll("\\+", " ");
            searchTerm = searchTerm.replaceAll("\\[", " ");
            searchTerm = searchTerm.replaceAll("\\]", " ");
        }


        Map<String, List<String>> preselectedOntologies = new HashMap<String, List<String>>();
        List msPreselectedParentTerms = new ArrayList<String>();
        msPreselectedParentTerms.add("MS:1000458");  // Source Description
        preselectedOntologies.put("PRIDE", null);
        preselectedOntologies.put("PSI", null);
        preselectedOntologies.put("MS", msPreselectedParentTerms);

        new OLSDialog(this, this, true, "instrumentSource", ontology, searchTerm, preselectedOntologies);
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_instrumentSourceJButtonActionPerformed

    /**
     * Opens the OLS dialog.
     *
     * @param evt
     */
    private void instrumentDetectorJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_instrumentDetectorJButtonActionPerformed
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        String searchTerm = null;
        String ontology = "Mass Spectroscopy CV (PSI-MS) [PSI]";

        if (instrumentDetectorJTextField.getText().length() > 0) {

            searchTerm = instrumentDetectorJTextField.getText();

            ontology = searchTerm.substring(searchTerm.lastIndexOf("[") + 1, searchTerm.lastIndexOf("]") - 1);
            ontology = getOntologyFromCvTerm(ontology);

            searchTerm = searchTerm.substring(0, searchTerm.indexOf("[") - 1);
            searchTerm = searchTerm.replaceAll("-", " ");
            searchTerm = searchTerm.replaceAll(":", " ");
            searchTerm = searchTerm.replaceAll("\\(", " ");
            searchTerm = searchTerm.replaceAll("\\)", " ");
            searchTerm = searchTerm.replaceAll("&", " ");
            searchTerm = searchTerm.replaceAll("\\+", " ");
            searchTerm = searchTerm.replaceAll("\\[", " ");
            searchTerm = searchTerm.replaceAll("\\]", " ");
        }

        new OLSDialog(this, this, true, "instrumentDetector", ontology, searchTerm);
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_instrumentDetectorJButtonActionPerformed

    /**
     * If the user double clicks on a row in the processing methods table the
     * OLS dialog is shown where the processing method can be altered. If the
     * user right clicks a pop up menu is shown for editing, or delting the
     * selected processing method.
     *
     * @param evt
     */
    private void processingMethodsJTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_processingMethodsJTableMouseClicked
        if (evt.getButton() == 3) {

            int row = processingMethodsJTable.rowAtPoint(evt.getPoint());
            int column = processingMethodsJTable.columnAtPoint(evt.getPoint());

            processingMethodsJTable.changeSelection(row, column, false, false);
            processingMethodsPopupJMenu.show(evt.getComponent(), evt.getX(), evt.getY());
        } else if (evt.getButton() == 1 && evt.getClickCount() == 2) {
            processingMethodsEditJMenuItemActionPerformed(null);
        }
}//GEN-LAST:event_processingMethodsJTableMouseClicked

    /**
     * If the delete key is pressed the selected rows in the processing methods
     * table is removed.
     *
     * @param evt
     */
    private void processingMethodsJTableKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_processingMethodsJTableKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
            processingMethodsDeleteSelectedRowJMenuItemActionPerformed(null);
        }
}//GEN-LAST:event_processingMethodsJTableKeyReleased

    /**
     * Opens the OLS dialog.
     *
     * @param evt
     */
    private void processingMethodsJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_processingMethodsJButtonActionPerformed
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
        new OLSDialog(this, this, true, "processingMethods", "Mass Spectroscopy CV (PSI-MS) [PSI]", null);
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_processingMethodsJButtonActionPerformed

    /**
     * Closes the example frame.
     *
     * @param evt
     */
    private void closeJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeJButtonActionPerformed
        System.exit(0);
    }//GEN-LAST:event_closeJButtonActionPerformed

    /**
     * Opens the OLS dialog.
     *
     * @param evt
     */
    private void processingMethodsEditJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_processingMethodsEditJMenuItemActionPerformed

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        int selectedRow = processingMethodsJTable.getSelectedRow();

        String searchTerm = (String) processingMethodsJTable.getValueAt(selectedRow, 1);

        String ontology = searchTerm.substring(searchTerm.lastIndexOf("[") + 1, searchTerm.lastIndexOf("]") - 1);
        ontology = getOntologyFromCvTerm(ontology);

        searchTerm = searchTerm.substring(0, searchTerm.indexOf("[") - 1);
        searchTerm = searchTerm.replaceAll("-", " ");
        searchTerm = searchTerm.replaceAll(":", " ");
        searchTerm = searchTerm.replaceAll("\\(", " ");
        searchTerm = searchTerm.replaceAll("\\)", " ");
        searchTerm = searchTerm.replaceAll("&", " ");
        searchTerm = searchTerm.replaceAll("\\+", " ");
        searchTerm = searchTerm.replaceAll("\\[", " ");
        searchTerm = searchTerm.replaceAll("\\]", " ");

        new OLSDialog(this, this, true, "processingMethods", ontology, selectedRow, searchTerm);
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_processingMethodsEditJMenuItemActionPerformed

    /**
     * Deletes the selected row in the processing methods table.
     *
     * @param evt
     */
    private void processingMethodsDeleteSelectedRowJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_processingMethodsDeleteSelectedRowJMenuItemActionPerformed

        int selectedRow = processingMethodsJTable.getSelectedRow();

        if (selectedRow != -1) {
            ((DefaultTableModel) processingMethodsJTable.getModel()).removeRow(selectedRow);
            fixProcessingMethodsIndices();
        }
}//GEN-LAST:event_processingMethodsDeleteSelectedRowJMenuItemActionPerformed

    /**
     * Opens the OLS dialog.
     *
     * @param evt
     */
    private void modificationMassJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modificationMassJButtonActionPerformed
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        boolean error = false;
        Double currentModificationMass = 0.0;
        Double currentAccuracy = 0.1;

        try {
            currentModificationMass = new Double(modificationMassJTextField.getText());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null,
                    "The mass is not a number!", "Modification Mass", JOptionPane.INFORMATION_MESSAGE);
            modificationMassJTextField.requestFocus();
            error = true;
        }

        if (!error) {
            try {
                currentAccuracy = new Double(modificationAccuracyJTextField.getText());

                if (currentAccuracy < 0) {
                    JOptionPane.showMessageDialog(null,
                            "The accuracy has to be a positive value.", "Mass Accuracy", JOptionPane.INFORMATION_MESSAGE);
                    modificationAccuracyJTextField.requestFocus();
                    error = true;
                }

            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null,
                        "The accuracy is not a number!", "Mass Accuracy", JOptionPane.INFORMATION_MESSAGE);
                modificationAccuracyJTextField.requestFocus();
                error = true;
            }
        }

        if (!error) {
            new OLSDialog(this, this, true, "modificationMass", "Protein Modifications (PSI-MOD) [MOD]", -1,
                    modificationNameJTextField.getText(), currentModificationMass, currentAccuracy,
                    OLSDialog.OLS_DIALOG_PSI_MOD_MASS_SEARCH, null);
        }

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_modificationMassJButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        setLookAndFeel();

        // open the OLS Dialog usage example frame
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new OLS_Example().setVisible(true);
            }
        });
    }

    /**
     * Fixes the indices in the processing method table so that they are in
     * accending order starting from one
     */
    private void fixProcessingMethodsIndices() {
        for (int row = 0; row < ((DefaultTableModel) processingMethodsJTable.getModel()).getRowCount(); row++) {
            ((DefaultTableModel) processingMethodsJTable.getModel()).setValueAt(new Integer(row + 1), row, 0);
        }
    }

    /**
     * Insert a new modification.
     *
     * @param name
     * @param accession
     * @param ontology
     */
    public void setModification(String name, String accession, String ontology) {
        modificationJTextField.setText(name + " [" + accession + "]");
        modificationJTextField.setCaretPosition(0);
    }

    /**
     * Insert a new instrument source.
     *
     * @param name
     * @param accession
     * @param ontology
     */
    public void setInstrumentSource(String name, String accession, String ontology) {
        instrumentSourceJTextField.setText(name + " [" + accession + "]");
        instrumentSourceJTextField.setCaretPosition(0);
    }

    /**
     * Insert a new instrument detector
     *
     * @param name
     * @param accession
     * @param ontology
     */
    public void setInstrumentDetector(String name, String accession, String ontology) {
        instrumentDetectorJTextField.setText(name + " [" + accession + "]");
        instrumentDetectorJTextField.setCaretPosition(0);
    }

    /**
     * Instert a new processing method.
     *
     * @param name
     * @param accession
     * @param ontology
     * @param modifiedRow The row to modify, use -1 if adding a new processing
     * method
     */
    public void addProcessingMethod(String name, String accession,
            String ontology, int modifiedRow) {
        addProcessingMethod(name, accession, ontology, null, modifiedRow);
    }

    /**
     * Instert a new processing method.
     *
     * @param name
     * @param accession
     * @param ontology
     * @param value
     * @param modifiedRow The row to modify, use -1 if adding a new processing
     * method
     */
    public void addProcessingMethod(String name, String accession,
            String ontology, String value, int modifiedRow) {

        if (value != null) {
            if (value.equalsIgnoreCase("null")) {
                value = null;
            }
        }

        if (modifiedRow == -1) {

            ((DefaultTableModel) this.processingMethodsJTable.getModel()).addRow(
                    new Object[]{
                        new Integer(processingMethodsJTable.getRowCount() + 1),
                        name + " [" + accession + "]", value
                    });

        } else {
            processingMethodsJTable.setValueAt(name + " [" + accession + "]", modifiedRow, 1);
            processingMethodsJTable.setValueAt(null, modifiedRow, 2);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel backgroundPanel;
    private javax.swing.JButton closeJButton;
    private javax.swing.JLabel detectorLabel;
    private javax.swing.JButton instrumentDetectorJButton;
    private javax.swing.JTextField instrumentDetectorJTextField;
    private javax.swing.JButton instrumentSourceJButton;
    private javax.swing.JTextField instrumentSourceJTextField;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JLabel massLabel;
    private javax.swing.JTextField modificationAccuracyJTextField;
    private javax.swing.JTextField modificationJTextField;
    private javax.swing.JButton modificationMassJButton;
    private javax.swing.JTextField modificationMassJTextField;
    private javax.swing.JTextField modificationNameJTextField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel plusMinusLabel;
    private javax.swing.JMenuItem processingMethodsDeleteSelectedRowJMenuItem;
    private javax.swing.JMenuItem processingMethodsEditJMenuItem;
    private javax.swing.JButton processingMethodsJButton;
    private javax.swing.JScrollPane processingMethodsJScrollPane;
    private javax.swing.JTable processingMethodsJTable;
    private javax.swing.JPopupMenu processingMethodsPopupJMenu;
    private javax.swing.JPanel psiModExamplePanel;
    private javax.swing.JLabel psiModLabel;
    private javax.swing.JLabel sourceLabel;
    private javax.swing.JPanel tableExamplePanel;
    private javax.swing.JPanel textFieldExamplePanel;
    // End of variables declaration//GEN-END:variables

    /**
     * @see OLSInputable
     */
    public void insertOLSResult(String field, String selectedValue,
            String accession,
            String ontologyShort, String ontologyLong, int modifiedRow,
            String mappedTerm,
            Map<String, String> metadata) {

        if (field.equalsIgnoreCase("instrumentSource")) {
            setInstrumentSource(selectedValue, accession, ontologyShort);
        } else if (field.equalsIgnoreCase("instrumentDetector")) {
            setInstrumentDetector(selectedValue, accession, ontologyShort);
        } else if (field.equalsIgnoreCase("processingMethods")) {
            addProcessingMethod(selectedValue, accession, ontologyShort, modifiedRow);
        } else if (field.equalsIgnoreCase("modificationMass")) {
            setModification(selectedValue, accession, ontologyShort);
        }
    }

    /**
     * @see OLSInputable
     */
    public Window getWindow() {
        return (Window) this;
    }

    /**
     * Tries to extract the ontology from the given cv term. For example
     * BTO:0000763 returns BTO.
     *
     * @param cvTerm the cv term to extract the ontology from, e.g., BTO:0000763
     * @return the extracted ontology
     */
    public static String getOntologyFromCvTerm(String cvTerm) {

        String ontology;

        if (cvTerm.lastIndexOf(":") != -1) {
            ontology = cvTerm.substring(0, cvTerm.lastIndexOf(":"));
        } else if (cvTerm.lastIndexOf("_") != -1) {
            ontology = cvTerm.substring(0, cvTerm.lastIndexOf("_"));
        } else {
            ontology = "NEWT";
        }

        return ontology;
    }
}
